//
//  AppDelegate.h
//
//
//
//  Copyright (c) 2014 Scott All rights reserved.
//

#import "RAViewController.h"
#import "RACollectionViewCell.h"
#import "NetHandler.h"
#import "WeatherViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "MapViewController.h"
#import "WeiZhangViewController.h"
#import "SearchViewController.h"
#import "EvaluationViewController.h"
#import "ForumViewController.h"
#import "NewCarsViewController.h"
#import "PhotoViewController.h"
#import "PersonalViewController.h"
#import "ForumThreeViewController.h"
#import "ForumArchiver.h"
#import "PersonalViewController.h"



@interface RAViewController ()

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *photosArray;
@property (nonatomic, retain) NSString *arrPath;


@end

@implementation RAViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self setupPhotosArray];
}


- (void)setupPhotosArray
{
    // 为了保证每次用户点开程序时， cell的排列是最后一次的排列顺序，数组要保存在本地，每次从本地加载。
    // 同时，cell重新移动之后， 更新一次本地数据库。
    
    // 创建沙盒 , 一次性添加， 程序之后就不需要再次创建。
    // 产品在上线的时候， 工程文件夹里面有一个沙盒，里面是创建好的数组
    
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentPath = [path lastObject];
    
    
    self.arrPath = [documentPath stringByAppendingString:@"/photoarray.xml"];
    
    
 
/*
    *******************************************************
    // 项目开始的时候向沙盒中添加一个数组
    // 之后沙盒中的数组不用初始化， 每次程序开启，都会从本地的沙盒中读取数据
 *******************************************************
 */
    NSMutableArray *phontArr = [NSMutableArray arrayWithObjects:@"1.jpg", @"2.jpg", @"3.jpg", @"4.jpg", @"5.jpg", @"6.jpg", @"7.jpg", @"8.jpg", @"9.jpg", nil];
 
    NSArray *array = [NSArray arrayWithContentsOfFile:self.arrPath];
    
    // 第一次运行的时候判断是否有数组。如果没有的话，写入数组
    if (array.count == 0) {
        [phontArr writeToFile:self.arrPath atomically:YES];
        
    }

    [_photosArray removeAllObjects];
    _photosArray = nil;
    
    // 使用 沙盒向_photoArray数组里面写入数据
    _photosArray = [NSMutableArray arrayWithContentsOfFile:self.arrPath];

    
//************************************************************************
    // 源码 向_phontArray数组里面写入数据
    
//    for (NSInteger i = 1; i <= 9; i++) {
//        NSString *photoName = [NSString stringWithFormat:@"%ld.jpg",(long)i];
//        UIImage *photo = [UIImage imageNamed:photoName];
//        [_photosArray addObject:photo];
//    }
    
//************************************************************************
   
    
}

// 点击Cell的动作
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0) {
        return;
    }
    if (_photosArray.count == 1) {
        return;
    }
    
    //**************************************************************
    //定制部分
    
    NSLog(@"%@", _photosArray);
    
    NSString *currentIndex = [_photosArray objectAtIndex:indexPath.item];
    
    NSLog(@"currentIndex:%@", currentIndex);
    
    // 进入天气预报界面
    if ([currentIndex isEqualToString: @"4.jpg"]) {
        
        WeatherViewController *weatherVC = [[WeatherViewController alloc] init];
        
        [weatherVC setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
        
        [self presentViewController:weatherVC animated:YES completion:^{
            // 进入天气预报界面要传的值
        }];
        
        
    }
    
    // 进入（地图）界面
    if ([currentIndex isEqualToString:@"6.jpg"]) {
        MapViewController *map = [[MapViewController alloc] init];
        
        [map setModalTransitionStyle:(UIModalTransitionStyleCrossDissolve)];
        
        [self presentViewController:map animated:YES completion:^{

        }];
        
    }
    
    // 进入违章查询界面
    if ([currentIndex isEqualToString:@"2.jpg"]) {
        WeiZhangViewController *weizhang = [[WeiZhangViewController alloc] init];
        
        [weizhang setModalTransitionStyle:(UIModalTransitionStyleCrossDissolve)];
        
        [self presentViewController:weizhang animated:YES completion:^{
            
        }];
    }
    
    // 进入找车界面
    if ([currentIndex isEqualToString:@"1.jpg"]) {
        
        SearchViewController *searchVC = [[SearchViewController alloc] init];
        
        UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:searchVC];
        
        [self presentViewController:navi animated:YES completion:^{
            
            
        }];
    }
    
    // 进入评测界面
    if ([currentIndex isEqualToString:@"3.jpg"]) {
        
        EvaluationViewController *evaVC = [[EvaluationViewController alloc] init];
        
        UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:evaVC];
        
        [self presentViewController:navi animated:YES completion:^{
            
            
        }];
        
            
        }
        
    // 进入论坛界面
    if ([currentIndex isEqualToString:@"7.jpg"]) {
        
        // 使用反归档
        // 判断沙盒中是否有进入主页的数据，如果有直接进入设置主页的论坛
        // 如果没有， 进入正常的论坛界面
        
        // 创建沙盒的路径
        // 建立沙盒
        NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        
        NSString *documentPath = [path lastObject];
        
        NSString *forumPath = [documentPath stringByAppendingString:@"/forumArchiver.aaa"];
      
        ForumArchiver *arch = [NSKeyedUnarchiver unarchiveObjectWithFile:forumPath];
        NSLog(@"%@", arch.archiverID);
        
        
        ForumViewController *forum = [[ForumViewController alloc] init];
        
        UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:forum];
        
        [self presentViewController:navi animated:YES completion:^{
            
        }];
        
        //判断是否进入我的论坛
        //
//        
//        if (arch.archiverID == nil) {
//            ForumViewController *forum = [[ForumViewController alloc] init];
//            
//            UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:forum];
//            
//            [self presentViewController:navi animated:YES completion:^{
//                
//            }];
//            
//        } else {
//            
//            ForumThreeViewController *forumThree = [[ForumThreeViewController alloc] init];
//            UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:forumThree];
//            
//            [self presentViewController:navi animated:YES completion:^{
//
//                forumThree.number = arch.archiverID;
//                forumThree.pageTitle = arch.archiverTitle;
//            }];
//            
//        }
        
    }
    
    // 进入新车界面
    if ([currentIndex isEqualToString:@"8.jpg"]) {
        NewCarsViewController *newcar = [[NewCarsViewController alloc] init];
        
        UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:newcar];
        
        [self presentViewController:navi animated:YES completion:^{
            
        }];
        
        
    }
    
    // 进入图赏界面
    if ([currentIndex isEqualToString:@"9.jpg"]) {
        PhotoViewController *phoho = [[PhotoViewController alloc] init];
        
        UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:phoho];
        
        [self presentViewController:navi animated:YES completion:^{
            
            
        }];
    }
    
    // 进入更多界面
    if ([currentIndex isEqualToString:@"5.jpg"]) {
        PersonalViewController *person = [[PersonalViewController alloc] init];
        UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:person];
        
        [self presentViewController:navi animated:YES completion:^{
            
            
        }];
    }
    
    //***********************************************************************************
    // 源码： 点击删除cell
    
    //    [self.collectionView performBatchUpdates:^{
    //        [_photosArray removeObjectAtIndex:indexPath.item];
    //        [self.collectionView deleteItemsAtIndexPaths:@[indexPath]];
    //    } completion:^(BOOL finished) {
    //        [self.collectionView reloadData];
    //    }];
    
    //***********************************************************************************
    
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    return _photosArray.count;
}

- (CGFloat)sectionSpacingForCollectionView:(UICollectionView *)collectionView
{
    return 5.f;
}

- (CGFloat)minimumInteritemSpacingForCollectionView:(UICollectionView *)collectionView
{
    return 5.f;
}

- (CGFloat)minimumLineSpacingForCollectionView:(UICollectionView *)collectionView
{
    return 5.f;
}

- (UIEdgeInsets)insetsForCollectionView:(UICollectionView *)collectionView
{
    return UIEdgeInsetsMake(5.f, 0, 5.f, 0);
}

// 第一个collectionView尺寸大小
- (CGSize)collectionView:(UICollectionView *)collectionView sizeForLargeItemsInSection:(NSInteger)section
{
    if (section == 0) {
        return CGSizeMake(320, 10);
    }
    return RACollectionViewTripletLayoutStyleSquare; //same as default !
}

- (UIEdgeInsets)autoScrollTrigerEdgeInsets:(UICollectionView *)collectionView
{
    return UIEdgeInsetsMake(50.f, 0, 50.f, 0); //Sorry, horizontal scroll is not supported now.
}

- (UIEdgeInsets)autoScrollTrigerPadding:(UICollectionView *)collectionView
{
    return UIEdgeInsetsMake(64.f, 0, 0, 0);
}

- (CGFloat)reorderingItemAlpha:(UICollectionView *)collectionview
{
    return .3f;
}

- (void)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout didEndDraggingItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self.collectionView reloadData];
}


// 移动设置
- (void)collectionView:(UICollectionView *)collectionView itemAtIndexPath:(NSIndexPath *)fromIndexPath didMoveToIndexPath:(NSIndexPath *)toIndexPath
{
    // 源码
    UIImage *image = [_photosArray objectAtIndex:fromIndexPath.item];
    [_photosArray removeObjectAtIndex:fromIndexPath.item];
    [_photosArray insertObject:image atIndex:toIndexPath.item];
    
    // 此处数组移动完毕， 应该将数组重新保存进本地中。

    [_photosArray writeToFile:self.arrPath atomically:YES];
    
    [self.collectionView reloadData];
    
}

// 首个Section 不移动
- (BOOL)collectionView:(UICollectionView *)collectionView itemAtIndexPath:(NSIndexPath *)fromIndexPath canMoveToIndexPath:(NSIndexPath *)toIndexPath
{
    if (toIndexPath.section == 0) {
        return NO;
    }
    return YES;
}


- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return NO;
    }
    return YES;
}


// 生成Cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.section == 0) {
        // 设置标头Cell
        static NSString *cellID = @"headerCell";
        UICollectionViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
        cell.backgroundColor = [UIColor whiteColor];
        return cell;
        
    }else {
        static NSString *cellID = @"cellID";
        RACollectionViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
        [cell.imageView removeFromSuperview];
        cell.imageView.frame = cell.bounds;
        
//        cell.imageView.image = _photosArray[indexPath.item];   源码
        
//*********************************************
// 定制部分
        
        cell.imageView.image = [UIImage imageNamed:_photosArray[indexPath.item]];
        
        [cell.contentView addSubview:cell.imageView];
        
        // cell的背景色为白色
        cell.backgroundColor = [UIColor whiteColor];
        
        return cell;
       
    }
}


// 自带的一个刷新代码， 不要点击一个按键
//- (IBAction)refresh:(UIBarButtonItem *)sender
//{
//    [self setupPhotosArray];
//    [self.collectionView reloadData];
//}


@end
